<?php

namespace App\Library\Service;

use App\Library\Logic\OperateLogLogic;
use App\Library\Logic\ShopProductAlbumLogic;
use App\Library\Logic\ShopUserLogic;
use App\Library\Requests\Base\SerialNumberRequest;
use App\Library\Requests\ProductAlbum\ProductAlbumAddRequest;
use App\Library\Requests\ProductAlbum\ProductAlbumEditRequest;
use App\Library\Requests\ProductAlbum\ProductAlbumPageRequest;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\JsonResponse;

class ShopProductAlbumService extends BaseService
{
    public function __construct(ShopProductAlbumLogic $logic)
    {
        $this->logic = $logic;
    }

    /**
     * 产品相册公开或私有
     * @param SerialNumberRequest $request
     * @return JsonResponse
     *
     */
    public function transShareStatus(SerialNumberRequest $request): JsonResponse
    {
        $model = $this->logic->findBySerialNumber($request);
        if (is_null($model)) return fail(433);
        $model->share_status = $model->share_status ? 0 : 1;
        $model->save();
        OperateLogLogic::add();
        return success();
    }

    /**
     * delete
     * @param SerialNumberRequest $request
     * @return JsonResponse
     *
     */
    public function delete(SerialNumberRequest $request): JsonResponse
    {
        $model = $this->logic->findBySerialNumber($request);
        if (is_null($model)) return fail(433);
        $model->delete();
        OperateLogLogic::add();
        return success();
    }

    /**
     * add
     * @param ProductAlbumAddRequest $request
     * @return JsonResponse
     *
     */
    public function add(ProductAlbumAddRequest $request): JsonResponse
    {
        $model = $this->logic->insertForModel(array_merge($request->all(), [
            'title' => mb_substr($request->describe, 0, 30),
            'publisherId' => USERID,
            'userMainId' => MAINID
        ]));
        OperateLogLogic::add();
        return success(['serial_number' => $model->serial_number]);
    }

    /**
     * edit
     * @param ProductAlbumEditRequest $request
     * @return JsonResponse
     *
     */
    public function edit(ProductAlbumEditRequest $request): JsonResponse
    {
        $model = $this->logic->findBySerialNumber($request);
        if (is_null($model)) return fail(433);
        $model->file_ids = $request->fileIds;
        $model->status_share = $request->statusShare;
        $model->describe = $request->describe;
        $model->title = mb_substr($request->describe, 0, 30);
        $model->desc_img_ids = $request->descImgIds;
        $model->tag_ids = $request->tagIds;
        $model->save();
        OperateLogLogic::add();
        return success();
    }

    /**
     * paging
     * @param ProductAlbumPageRequest $request
     * @return JsonResponse
     *
     */
    public function paging(ProductAlbumPageRequest $request): JsonResponse
    {
        return success($this->logic->paging($request));
    }

    /**
     * detail
     * @param SerialNumberRequest $request
     * @return JsonResponse
     *
     */
    public function detail(SerialNumberRequest $request): JsonResponse
    {
        $model = $this->logic->findBySerialNumber($request);
        if (is_null($model)) return failMessage('该相册已被删除');
        $user = ShopUserLogic::findByWhere(['user_id' => $model->user_main_id]);
        $model->user_shop_name = $user->user_shop_name;
        $model->user_avatar = $user->user_avatar;
        $model->user_nickname = $user->user_nickname;
        return success($model);
    }

    /**
     *
     * @param string|null $segment
     * @return Model|bool
     *
     */
    public function productAlbumDisplay(?string $segment): Model|bool
    {
        $model = $this->logic->query()
            ->select(
                'shop_product_album.*',
                'user_shop_name',
                'user_avatar',
            )
            ->leftJoin('shop_user', 'shop_user.user_id', 'shop_product_album.user_main_id')
            ->where('shop_product_album.serial_number', $segment)->first();
        if (is_null($model)) return false;
        else return $model;
    }
}
